package com.cooper.controllers;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.cooper.constants.AppConstants;
import com.cooper.core.MinasPropertyHandler;
import com.cooper.entity.DisplayPicConfigListDo;
import com.cooper.pagination.PageInfo;
import com.cooper.pagination.PageResult;
import com.cooper.service.admin.DisplayPicConfigService;
import com.cooper.service.admin.LocationService;
import com.cooper.service.admin.MallCategoryService;
import com.cooper.util.Controllers;
import com.cooper.vo.DisplayPicConfigVo;
import com.cooper.vo.LocationVo;
import com.cooper.vo.MallCategoryVo;
import com.google.common.collect.Maps;

import net.paoding.rose.web.ControllerInterceptorAdapter;
import net.paoding.rose.web.Invocation;

/**
 * 全局拦截器
 * @Date: Feb 14, 2014 8:18:22 PM<br>
 * @Copyright (c) 2014 MiXuan <br> * 
 * @since 1.0
 * @author coral
 */
public class GlobalInterceptor extends ControllerInterceptorAdapter {
	
	@Autowired
	private MinasPropertyHandler minasPropertyHandler;

	@Autowired
	private MallCategoryService mallCategoryService;
	
	@Autowired
	private DisplayPicConfigService displayPicConfigService;
	
	@Autowired
	LocationService locationService;
	
	@Override
	protected Object before(Invocation inv) throws Exception {
		inv.addModel(AppConstants.ACTION_PATH, inv.getRequestPath().getUri());
		inv.addModel("fileServerBasePath", this.minasPropertyHandler.getFileServer());
		
		if(null == Controllers.getServletContext(inv, "firstLevelCategoryList")){
			List<MallCategoryVo> firstLevelCategoryList = this.mallCategoryService.getAllCategoryWithChilds();
			if(firstLevelCategoryList!=null&&!firstLevelCategoryList.isEmpty()){
				for(MallCategoryVo vo:firstLevelCategoryList){
					if(vo.getParentId()==1){//一级类目取三张上传的图片
						DisplayPicConfigVo queryParamVo = new DisplayPicConfigVo();
						queryParamVo.setStatus(DisplayPicConfigListDo.DISPLAY_YES);
						queryParamVo.setDisType("mallcategory"+vo.getId());
						PageInfo pageInfo = new PageInfo();
						pageInfo.setPageIndex(1);
						pageInfo.setPageSize(3);
						PageResult<DisplayPicConfigVo> ret = this.displayPicConfigService.queryDisplayPicConfigPage(queryParamVo, pageInfo);
						if(ret!=null&&ret.getResult()!=null){
							vo.setDisplayPicConfigList(ret.getResult());
						}
					}
				}
			}
			inv.getServletContext().setAttribute("firstLevelCategoryList", firstLevelCategoryList);
		}
		
		if(null == Controllers.getServletContext(inv, "firstLevelLocationMap")){
			Map<String, LocationVo> firstLevelLocationMap = Maps.newLinkedHashMap();
			List<LocationVo> locations = locationService.getAllLocationWithChilds("en_US");
			if(locations!=null&&!locations.isEmpty()){
				for(LocationVo lo:locations){
					DisplayPicConfigVo queryParamVo = new DisplayPicConfigVo();
					queryParamVo.setStatus(DisplayPicConfigListDo.DISPLAY_YES);
					queryParamVo.setDisType("location"+lo.getId());
					PageInfo pageInfo = new PageInfo();
					pageInfo.setPageIndex(1);
					pageInfo.setPageSize(3);
					PageResult<DisplayPicConfigVo> ret = this.displayPicConfigService.queryDisplayPicConfigPage(queryParamVo, pageInfo);
					if(ret!=null&&ret.getResult()!=null){
						lo.setDisplayPicConfigList(ret.getResult());
					}
					firstLevelLocationMap.put(lo.getEnUs().replace("\\s", ""), lo);
				}
			}
			inv.getServletContext().setAttribute("firstLevelLocationMap", firstLevelLocationMap);
		}
		return super.before(inv);
	}
	
}
